package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccountOverall;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.ApproveAccountService;
import csc.fresher.service.DepositAccountService;
import csc.fresher.service.MarkRemoveAccService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class MarkRemoveAccManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private DepositAccountService depositManageService;

	@Autowired
	private ApproveAccountService approveAccountService;

	@Autowired
	private MarkRemoveAccService markRemoveAccService;

	@Autowired
	private SystemLoginManageBean systemLoginManageBean;

	@Autowired
	private SystemUserService systemUserService;

	private String selectedAccountType;
	private List<CustomerAccountOverall> depositAccountList;
	private String stateChoosed;
	private CustomerOverallDataModel accountListDataModel;
	private CustomerAccountOverall customerAccountOverall;
	private Boolean selectAll;
	private Map<Long, Boolean> selectedCheckbox = new HashMap<Long, Boolean>();
	private Boolean checkAllCheckbox;
	private String resultCheckAll;
	private List<CustomerAccountOverall> selectedCustomers;

	public void onLoad() {
		SystemUser systemUser;

		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			boolean result = checkRole(listrole);
			if (result == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("AccountManagement.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean checkRole(List<Long> listRole) {

		for (int i = 0; i < listRole.size(); i++) {
			if (3 == listRole.get(i)) {
				return true;
			}
		}
		return false;
	}

	@PostConstruct
	public void init() {
		this.customerAccountOverall = null;
		this.depositAccountList = firstLoad();
		this.accountListDataModel = new CustomerOverallDataModel(
				this.depositAccountList);
		this.resultCheckAll = "false";
	}

	// first load
	public List<CustomerAccountOverall> firstLoad() {
		depositAccountList = depositManageService.getAccountByState("disable",
				"DepositAccount");
		return depositAccountList;
	}

	// edit button
	public String approve() {
		if ("SavingAccount".equals(selectedAccountType)) {
			for (CustomerAccountOverall c : selectedCustomers) {
				Customer customer = new Customer();
				customer.setCustomerID(c.getCustomerID());
				markRemoveAccService.markCustomerAsRemovable(customer);
			}
			depositAccountList = depositManageService.getAccountByState(
					"disable", "SavingAccount");
			this.accountListDataModel = new CustomerOverallDataModel(
					this.depositAccountList);
		} else if ("DepositAccount".equals(selectedAccountType)) {
			for (CustomerAccountOverall c : accountListDataModel) {
				Customer customer = new Customer();
				customer.setCustomerID(c.getCustomerID());
				markRemoveAccService.markCustomerAsRemovable(customer);
				selectedCheckbox.put(c.getAccountNumber(), false);
			}
			depositAccountList = depositManageService.getAccountByState(
					"disable", "DepositAccount");
			this.accountListDataModel = new CustomerOverallDataModel(
					this.depositAccountList);
		}
		checkAllCheckbox = false;
		resultCheckAll = "false";
		return "markRemovableAccount";
	}

	// select account type changed
	public void selectAccountTypeChanged(ValueChangeEvent e) {
		depositAccountList = depositManageService.getAccountByState("disable",
				e.getNewValue().toString());
		this.accountListDataModel = new CustomerOverallDataModel(
				this.depositAccountList);
		selectedAccountType = e.getNewValue().toString();
	}

	public void checkAllChekboxes(ValueChangeEvent ex) {
		if (ex.getNewValue().toString().equals("true")) {
			resultCheckAll = "true";

		} else {
			resultCheckAll = "false";
		}

	}

	// getter setter
	public List<CustomerAccountOverall> getDepositAccountList() {
		return depositAccountList;
	}

	public void setDepositAccountList(
			List<CustomerAccountOverall> depositAccountList) {
		this.depositAccountList = depositAccountList;
	}

	public CustomerAccountOverall getCustomerAccountOverall() {
		return customerAccountOverall;
	}

	public void setCustomerAccountOverall(CustomerAccountOverall customerAccount) {
		this.customerAccountOverall = customerAccount;
	}

	public String getStateChoosed() {
		return stateChoosed;
	}

	public void setStateChoosed(String stateChoosed) {
		this.stateChoosed = stateChoosed;
	}

	public String getSelectedAccountType() {
		return selectedAccountType;
	}

	public void setSelectedAccountType(String selectedAccountType) {
		this.selectedAccountType = selectedAccountType;
	}

	public Boolean getSelectetAll() {
		return selectAll;
	}

	public void setSelectedCheckbox(Boolean selectAll) {
		this.selectAll = selectAll;
	}

	public Map<Long, Boolean> getSelectedCheckbox() {
		return selectedCheckbox;
	}

	public void setSelectedCheckbox(Map<Long, Boolean> selectedCheckbox) {
		this.selectedCheckbox = selectedCheckbox;
	}

	public Boolean getCheckAllCheckbox() {
		return checkAllCheckbox;
	}

	public void setCheckAllCheckbox(Boolean checkAllCheckbox) {
		this.checkAllCheckbox = checkAllCheckbox;
		if (checkAllCheckbox) {
			for (CustomerAccountOverall c : accountListDataModel) {
				this.selectedCheckbox.put(c.getAccountNumber(), true);
			}
		} else {
			for (CustomerAccountOverall c : accountListDataModel) {
				this.selectedCheckbox.put(c.getAccountNumber(), false);
			}
		}
	}

	public String getResultCheckAll() {
		return resultCheckAll;
	}

	public void setResultCheckAll(String resultCheckAll) {
		this.resultCheckAll = resultCheckAll;
	}

	public List<CustomerAccountOverall> getSelectedCustomers() {
		return selectedCustomers;
	}

	public void setSelectedCustomers(
			List<CustomerAccountOverall> selectedCustomers) {
		this.selectedCustomers = selectedCustomers;
	}

	public CustomerOverallDataModel getAccountListDataModel() {
		return accountListDataModel;
	}

	public void setAccountListDataModel(
			CustomerOverallDataModel accountListDataModel) {
		this.accountListDataModel = accountListDataModel;
	}

}
